﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Siemens.Engineering;
using Siemens.Engineering.HW;
using Siemens.Engineering.HW.Features;
using Siemens.Engineering.SW;
using Siemens.Engineering.SW.Blocks;
using Siemens.Engineering.SW.ExternalSources;
using Siemens.Engineering.SW.Tags;
using Siemens.Engineering.SW.Types;
using Siemens.Engineering.Hmi;
using HmiTarget = Siemens.Engineering.Hmi.HmiTarget;
using Siemens.Engineering.Hmi.Tag;
using Siemens.Engineering.Hmi.Screen;
using Siemens.Engineering.Hmi.Cycle;
using Siemens.Engineering.Hmi.Communication;
using Siemens.Engineering.Hmi.Globalization;
using Siemens.Engineering.Hmi.TextGraphicList;
using Siemens.Engineering.Hmi.RuntimeScripting;
using Siemens.Engineering.Compiler;
using Siemens.Engineering.Library;
using System.IO;

namespace TIA_Openness
{
    public static class ExportScreensHelper
    {

        /// <summary>
        /// 导出所有画面
        /// </summary>
        /// <param name="rootPath"></param>
        /// <param name="hmitarget"></param>
        public static void ExportScreensOfDevice(string rootPath, HmiTarget hmitarget)
        {
            DirectoryInfo info = new DirectoryInfo(rootPath);
            info.Create();
            //export the ScreenFolder recursive
            string screenPath = Path.Combine(rootPath, "Screens");
            info = new DirectoryInfo(screenPath);
            info.Create();
            ExportScreens(screenPath, hmitarget);
        }

        private static void ExportScreens(string screenPath, HmiTarget target)
        {
            foreach (Screen screen in target.ScreenFolder.Screens)
            {
                screen.Export(new FileInfo(Path.Combine(screenPath, screen.Name + ".xml")), ExportOptions.WithDefaults);
            }
            foreach (ScreenUserFolder subfolder in target.ScreenFolder.Folders)
            {
                ExportScreenUserFolder(Path.Combine(screenPath, subfolder.Name), subfolder);
            }
        }
        private static void ExportScreenUserFolder(string screenPath, ScreenUserFolder folder)
        {
            foreach (Screen screen in folder.Screens)
            {
                screen.Export(new FileInfo(Path.Combine(screenPath, screen.Name + ".xml")), ExportOptions.WithDefaults);
            }
            foreach (ScreenUserFolder subfolder in folder.Folders)
            {
                ExportScreenUserFolder(Path.Combine(screenPath, subfolder.Name), subfolder);
            }
        }

        private static void ImportScreensToHMITarget(HmiTarget hmitarget)
        {
            FileInfo[] exportedScreens = new FileInfo[] {new
        FileInfo(@"D:\Samples\Import\Screen_1.xml"), new
        FileInfo(@"D:\Samples\Import\Screen_2.xml")};
            ScreenUserFolder folder = hmitarget.ScreenFolder.Folders.Find("MyScreenFolder");
            foreach (FileInfo screenFileInfo in exportedScreens)
            {
                folder.Screens.Import(screenFileInfo, ImportOptions.Override);
            }
        }

    }
}
